package com.example.learning_store;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

public class MainActivity3 extends AppCompatActivity {

    Button bt_create_db;
    Button bt_query;
    Button bt_inset;
    Button bt_UpData_db;
    Button bt_delete_db;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main3);
        initView();
        setOnClick();

    }
    private void initView(){
        bt_create_db=findViewById(R.id.bt_create_db);
        bt_query=findViewById(R.id.bt_query);
        bt_inset=findViewById(R.id.bt_inset);
        bt_UpData_db=findViewById(R.id.bt_UpData_db);
        bt_delete_db=findViewById(R.id.bt_delete_db);

    }
    private void setOnClick(){

        bt_create_db.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SQLiteOpenHelper Helper=MySQliteOpenHelper.getInstance(MainActivity3.this);
                SQLiteDatabase ReadDataBase=Helper.getReadableDatabase();
            }
        });
        /*
        查询
         */
        bt_query.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SQLiteOpenHelper Helper=MySQliteOpenHelper.getInstance(MainActivity3.this);
                SQLiteDatabase db=Helper.getReadableDatabase();
                if (db.isOpen()){
                    @SuppressLint("Recycle") Cursor cursor = db.rawQuery("select * from persons",null);
                    while (cursor.moveToNext()){
                        @SuppressLint("Range") int _id=cursor.getInt(cursor.getColumnIndex("_id"));
                        @SuppressLint("Range") String name= cursor.getString(cursor.getColumnIndex("name"));
                        Log.d("lxh","query:_id"+_id+"name:"+name);
                    }
                    //游标用完记得关闭否者耗费内存
                    cursor.close();
                    db.close();//数据库也需要关闭
                }
            }
        });
        /*
        增加
         */
        bt_inset.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SQLiteOpenHelper Helper=MySQliteOpenHelper.getInstance(MainActivity3.this);
                SQLiteDatabase db=Helper.getWritableDatabase();
                if (db.isOpen()){
                    String sql="insert into persons(name) values('lxhsb')";
                    db.execSQL(sql);
                    db.close();
                }
            }
        });

        /*
        改
         */
        bt_UpData_db.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SQLiteOpenHelper Helper=MySQliteOpenHelper.getInstance(MainActivity3.this);
                SQLiteDatabase db=Helper.getWritableDatabase();
                if (db.isOpen()){
                    String sql="update persons set name=? where _id=?";
                    db.execSQL(sql,new Object[]{"李连杰",5});
                    db.close();
                }
            }
        });
        /*
        删除
         */
        bt_delete_db.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SQLiteOpenHelper Helper=MySQliteOpenHelper.getInstance(MainActivity3.this);
                SQLiteDatabase db=Helper.getWritableDatabase();
                if(db.isOpen()){
                    String sql="delete from persons where _id=?";
                    db.execSQL(sql,new Object[]{4});
                    db.close();
                }
            }
        });
    }
}